//
//  HomeViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/7.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeHeadView.h"
#import "HomeTableViewCell.h"
#import "SayViewController.h"
#import "RentHouseViewController.h"
#import "WantPeopleViewController.h"
#import "AboutViewController.h"
#import "NewsInfoViewController.h"
#import "HomeNewsModel.h"
#import "HomeOnePitcureTableViewCell.h"
#import "WeatherViewController.h"
#import "MarryViewController.h"
#import "HealthViewController.h"
#import "HistoryViewController.h"

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<HomeNewsModel *> * homeNewsArray;

@end

@implementation HomeViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view .backgroundColor = WHITE_COLOR;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ClickHeaderViewButton:) name:@"ClickHeaderViewButton" object:nil];
    
    [HomeNewsModel homeNewsWithSucess:^(NSArray<HomeNewsModel *> *array) {
        
        self.homeNewsArray = array;
    } error:^{
        
    }];
    
    [self setupUI];
}

-(void)setHomeNewsArray:(NSArray<HomeNewsModel *> *)homeNewsArray{
    
    _homeNewsArray = homeNewsArray;
    
    [self.tableView reloadData];
}

- (void)setupUI{
    
//    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
//
//    view.backgroundColor = [UIColor redColor];
//
//    [self.view addSubview:view];
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
//    tableView.rowHeight = 125 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


    self.tableView = tableView;

    [self.tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"HomeListCell"];
    [self.tableView registerClass:[HomeOnePitcureTableViewCell class] forCellReuseIdentifier:@"HomeOneListCell"];
    [self.tableView registerClass:[HomeHeadView class] forHeaderFooterViewReuseIdentifier:@"HomeView"];

    [self.view addSubview:self.tableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.homeNewsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeNewsModel * model = self.homeNewsArray[indexPath.row];
    
    if (model.images.count == 3) {
        
        HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeListCell" forIndexPath:indexPath];
        
        cell.model = model;
        
        return cell;
    }else{
        
        HomeOnePitcureTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeOneListCell" forIndexPath:indexPath];
        
        cell.model = model;
        
        return cell;
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 450* SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HomeHeadView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HomeView"];
    
    return header;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeNewsModel * model = self.homeNewsArray[indexPath.row];
    
    if (model.images.count == 1) {
        
        return 103 * SCREENH6;
    }else{
        
        
        return 145 * SCREENH6;
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    HomeNewsModel * model = self.homeNewsArray[indexPath.row];
    
    NewsInfoViewController * newsVC = [NewsInfoViewController new];
    
    newsVC.ID = model.ID;
    
    [self.navigationController pushViewController:newsVC animated:YES];
    
    SHOW_TAB;
}


- (void)ClickHeaderViewButton:(NSNotification *)noti{
    
    NSString * type = noti.userInfo[@"type"];
    
    HIDDEN_TAB;
    
    if ([type isEqualToString:@"one"]) {
        
        SayViewController * sayVC = [SayViewController new];
        
        [self.navigationController pushViewController:sayVC animated:YES];
    }else if ([type isEqualToString:@"two"]){
        
        RentHouseViewController * rentVC = [RentHouseViewController new];
        
        [self.navigationController pushViewController:rentVC animated:YES];
    }else if ([type isEqualToString:@"three"]){
        
        WantPeopleViewController * wantVC = [WantPeopleViewController new];
        
        [self.navigationController pushViewController:wantVC animated:YES];
    }else if ([type isEqualToString:@"four"]){
        
        AboutViewController * aboutVC = [AboutViewController new];
        
        [self.navigationController pushViewController:aboutVC animated:YES];
    }else if ([type isEqualToString:@"five"]){
        
        WeatherViewController * weatherVC = [WeatherViewController new];
        
        [self.navigationController pushViewController:weatherVC animated:YES];
    }else if ([type isEqualToString:@"six"]){
        
        MarryViewController * marryVC = [MarryViewController new];
        
        [self.navigationController pushViewController:marryVC animated:YES];
    }else if ([type isEqualToString:@"seven"]){
        
        HealthViewController * healthVC = [HealthViewController new];
        
        [self.navigationController pushViewController:healthVC animated:YES];
    }else if ([type isEqualToString:@"eight"]){
        
        HistoryViewController * historyVC = [HistoryViewController new];
        
        [self.navigationController pushViewController:historyVC animated:YES];
    }
    
    SHOW_TAB;
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
